package SportsBot::NFL;

use strict;
use LWP::Simple;

# Returns string: the 
sub getLeagueTable
{
    # National Football League (NFL)
    #
    # Table URL: http://www.nfl.com/standings

    my $input = get("http://www.nfl.com/standings");
    $input =~ s/[\n\r\t]//g;
    $input =~ s/^.*<table class=\"standard_table w720px\" cellspacing=\"1\">(.*)<\!-- mraw --><\/table>.*$/$1/;
    $input =~ s/<tr[^>]*>/<tr>/g;
    $input =~ s/<\/?(center|a|th)[^>]*>//g;
    $input =~ s/<tr>TeamWLTPctPFPANet PtsTDHomeRoadDivPctConfPctNon-ConfStreakLast 5<\/tr>//g;
    $input =~ s/<\/td><td>/ || /g;
    $input =~ s/<\/td><\/tr>    <tr><td class=\"first_cell\">/\n/g;
    $input .= "\n";
    $input =~ s/<.*\n/\n/g;
    $input =~ s/^\n//;
    my @lines = split(/\n/, $input);

    my ($full, $mini, $counter, $rowclass);
    $counter = 1;

    $full = <<EOF;
{| cellpadding="5" cellspacing="0" class="standingstable"
|- align="center" class="standingssection"
| colspan="18" | '''[[w:National Football League|National Football League]]'''
EOF
    $mini = <<EOF;
{| border=1 cellpadding=3 cellspacing=0 style="margin: 1em 1em ; background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse; font-size: 95%;"    
|- style="background: #e9e9e9"   
! Conference !! Team !! W !! L !! Pct !! Streak | L5
EOF
    foreach (@lines) {
        $_ =~ s/^([A-Za-z \.49]*) \|\|/[[w:$1|$1]] ||/;
        # AFC
        if ($counter == 1) {
            $mini .= "|-\n| AFC East || " . miniaturizeRow($_) . "\n";
            $full .= <<EOF;
|- align="center" class="standingssubsection"
| colspan="18" | '''AFC East'''
|- class="standingsheader"
| Team || W || L || T || Pct. || PF || PA || Net Pts || TD || Home || Road || Div || Pct || Conf || Pct || Non-Conf || Streak || Last 5
EOF
        } elsif ($counter == 5) {
            $mini .= "|-\n| AFC North || " . miniaturizeRow($_) . "\n";
            $full .= <<EOF;
|- align="center" class="standingssubsection"
| colspan="18" | '''AFC North'''
|- class="standingsheader"
| Team || W || L || T || Pct. || PF || PA || Net Pts || TD || Home || Road || Div || Pct || Conf || Pct || Non-Conf || Streak || Last 5
EOF
        } elsif ($counter == 9) {
            $mini .= "|-\n| AFC South || " . miniaturizeRow($_) . "\n";
            $full .= <<EOF;
|- align="center" class="standingssubsection"
| colspan="18" | '''AFC South'''
|- class="standingsheader"
| Team || W || L || T || Pct. || PF || PA || Net Pts || TD || Home || Road || Div || Pct || Conf || Pct || Non-Conf || Streak || Last 5
EOF
        } elsif ($counter == 13) {
            $mini .= "|-\n| AFC West || " . miniaturizeRow($_) . "\n";
            $full .= <<EOF;
|- align="center" class="standingssubsection"
| colspan="18" | '''AFC West'''
|- class="standingsheader"
| Team || W || L || T || Pct. || PF || PA || Net Pts || TD || Home || Road || Div || Pct || Conf || Pct || Non-Conf || Streak || Last 5
EOF
        } elsif ($counter == 17) {
            $mini .= "|-\n| NFC East || " . miniaturizeRow($_) . "\n";
            $full .= <<EOF;
|- align="center" class="standingssubsection"
| colspan="18" | '''NFC East'''
|- class="standingsheader"
| Team || W || L || T || Pct. || PF || PA || Net Pts || TD || Home || Road || Div || Pct || Conf || Pct || Non-Conf || Streak || Last 5
EOF
        } elsif ($counter == 21) {
            $mini .= "|-\n| NFC North || " . miniaturizeRow($_) . "\n";
            $full .= <<EOF;
|- align="center" class="standingssubsection"
| colspan="18" | '''NFC North'''
|- class="standingsheader"
| Team || W || L || T || Pct. || PF || PA || Net Pts || TD || Home || Road || Div || Pct || Conf || Pct || Non-Conf || Streak || Last 5
EOF
        } elsif ($counter == 25) {
            $mini .= "|-\n| NFC South || " . miniaturizeRow($_) . "\n";
            $full .= <<EOF;
|- align="center" class="standingssubsection"
| colspan="18" | '''NFC South'''
|- class="standingsheader"
| Team || W || L || T || Pct. || PF || PA || Net Pts || TD || Home || Road || Div || Pct || Conf || Pct || Non-Conf || Streak || Last 5
EOF
        } elsif ($counter == 29) {
            $mini .= "|-\n| NFC West || " . miniaturizeRow($_) . "\n";
            $full .= <<EOF;
|- align="center" class="standingssubsection"
| colspan="18" | '''NFC West'''
|- class="standingsheader"
| Team || W || L || T || Pct. || PF || PA || Net Pts || TD || Home || Road || Div || Pct || Conf || Pct || Non-Conf || Streak || Last 5
EOF
        }
        $rowclass = ($counter & 1 ? "standingsrow" : "standingsrow2");
        $full .= "|- class=\"$rowclass\"\n| class=\"team\" | $_\n";
        $counter++;
    }
    chop $full; chop $mini;
    my $formatted = <<EOF;
<noinclude>
$full
|}
</noinclude><includeonly>
$mini
|}

[[Template:NFL|Full NFL Table]]
</includeonly>
<small>Last updated: {{subst:#time:F j, Y g:ia|-4 hours}} ET</small>
EOF
    return $formatted;
}

# Takes a standings table row and shrinks it down for the miniature version
# arg:row     - a standings table row
# return      - a shortened standings table row
sub miniaturizeRow
{
    # Regular row: Team | W | L | T | Pct | PF | PA | PD | TD | Home | Road | Div | Pct | Conf | Pct | Non Conf | Streak | L5
    # Shorter row: Team | W | L | Pct | Streak | L5
    my $row = shift;
    $row =~ s/([0-9]+) \|\| ([0-9]+) \|\| [0-9]+ \|\| ([0-9\.]+) \|\| [0-9]+ \|\| [0-9]+ \|\| [0-9\-]+ \|\| [0-9]+ \|\| [0-9]+-[0-9]+ \|\| [0-9]+-[0-9]+ \|\| [0-9]+-[0-9]+ \|\| [0-9\.]+ \|\| [0-9]+-[0-9]+ \|\| [0-9\.]+ \|\| [0-9]+-[0-9]+ \|\| ([0-5WL]+) \|\| ([0-9]+-[0-9]+)/$1 || $2 || $3 || $4 || $5/;
    return $row;
}

1;

